<?php

class Db_Tables2_User
{
	function __construct() {
		$this->hxb_db = Zend_Registry::get("hxb_db");
		$this->dealers_db = Zend_Registry::get("dealers_db");
	}
	
	function GetUsersByRole($role_id)
	{
		$result = array();
		
		$select = $this->hxb_db->select();
		$select->from("auth_emp_position as r", array("empId"));
		$select->joinLeft("auth_emp as e", "e.id=r.empId", array("realName"));
		
		if(is_array($role_id))
		{
			$select->where("r.positionId IN (?)", $role_id);
		}else{
			$select->where("r.positionId = ?", $role_id);
		}
		
		$data = $this->hxb_db->fetchAll($select);
		
		if(!empty($data))
		{
			foreach($data as $d)
			{
				$result[$d['empId']] = $d['realName'];
			}
		}
		
		return $result;
	}
	
	function GetMyStaffs($emp_id)
	{
		$select = $this->hxb_db->select();
		$select->from("auth_emp", array("id", "realName"));
		$select->where("parentEmpId = ?", intval($emp_id));
		$rows = $this->hxb_db->fetchAll($select);
		
		$result = array();
		
		if(!empty($rows))
		{
			foreach($rows as $row)
			{
				$result[$row['id']] = $row['realName'];
			}
		}
		
		return $result;
	}
	
	function GetStaffNames($emp_id_arr)
	{
		$result = array();
		
		if(!empty($emp_id_arr))
		{
			$select = $this->hxb_db->select();
			$select->from("auth_emp", array("id", "realName"));
			$select->where("id IN (?)", $emp_id_arr);
			$rows = $this->hxb_db->fetchAll($select);

			if(!empty($rows))
			{
				foreach($rows as $row)
				{
					$result[$row['id']] = $row['realName'];
				}
			}
		}
		
		return $result;
	}
	
	function GetMyStaffIds($emp_id)
	{
		$result = array($emp_id);
		
		if($emp_id)
		{
			$arr = array($emp_id);
			
			while(!empty($arr))
			{
				$select = $this->hxb_db->select();
				$select->from("auth_emp", array("id"));
				$select->where("parentEmpId IN (?)", $arr);
				$rows = $this->hxb_db->fetchAll($select);
				
				$arr = array();
				
				if(!empty($rows))
				{
					foreach($rows as $row)
					{
						$result[] = $row['id'];
						$arr[] = $row['id'];
					}
				}
			}
		}
		
		$result = array_unique($result);
		
		return $result;
	}
	
	function StaffCount($emp_id)
	{
		$ct = 0;
		
		if($emp_id)
		{
			$arr = array($emp_id);
			
			while(!empty($arr))
			{
				$select = $this->hxb_db->select();
				$select->from("auth_emp", array("id"));
				$select->where("parentEmpId IN (?)", $arr);
				$rows = $this->hxb_db->fetchAll($select);
				
				$arr = array();
				
				if(!empty($rows))
				{
					foreach($rows as $row)
					{
						$ct += 1;
						$arr[] = $row['id'];
					}
				}
			}
		}
		
		return $ct;
	}
	
	function MyHxbDealerIds($emp_id)
	{
		$dealer_ids = array();
		
		if($emp_id)
		{
			$select = $this->dealers_db->select();
			$select->from("b_bu_auth", "*");
			$select->where("emp_id = ?", $emp_id);
			$row = $this->dealers_db->fetchRow($select);
			
			if($row)
			{
				$group_id_arr = explode("|", $row['group_id_txt']);
				$city_id_arr = explode("|", $row['city_id_txt']);
				$factory_id_arr = explode("|", $row['factory_id_txt']);
				
				//筛选开始
				$select = $this->dealers_db->select();
				$select->from("t_dealer", "id");
				
				if(!empty($group_id_arr) && $group_id_arr[0])
				{
					$select->where("groupId IN (?)", $group_id_arr);
				}
				
				if(!empty($city_id_arr) && $city_id_arr[0])
				{
					$select->where("cityId IN (?)", $city_id_arr);
				}
				
				$rows = $this->dealers_db->fetchAll($select);
				
				foreach($rows as $row)
				{
					$dealer_ids[] = $row['id'];
				}
				
				if(!empty($dealer_ids) && !empty($factory_id_arr) && $factory_id_arr[0])
				{
					$select = $this->dealers_db->select();
					$select->from("t_dealer_factory", "dealerId");
					$select->where("factoryId IN (?)", $factory_id_arr);
					$select->where("dealerId IN (?)", $dealer_ids);
					$rows = $this->dealers_db->fetchAll($select);
					
					$dealer_ids = array();
					
					if($rows)
					{
						foreach($rows as $row)
						{
							$dealer_ids[] = $row['dealerId'];
						}
					}
				}
			}
		}
		
		return $dealer_ids;
	}
	
	/**
	 * array(
	 *	"group_id" =>
	 *	"province_id" =>
	 *	"city_id" =>
	 *	"factory_id" =>
	 * )
	 */
	
	function GetFilteredDealerIds($arr, $emp_auth_row)
	{
		$my_group_id_arr = $my_factory_id_arr = $my_city_id_arr = array();
		
		if($emp_auth_row)
		{
			$my_group_id_arr = explode("|", $emp_auth_row['group_id_txt']);
			$my_factory_id_arr = explode("|", $emp_auth_row['factory_id_txt']);
			$my_city_id_arr = explode("|", $emp_auth_row['city_id_txt']);
		}
		
		$dealer_ids = array();
		
		if(1)
		{
			if(1)
			{
				//筛选开始
				$select = $this->dealers_db->select();
				$select->from("t_dealer", "id");
				
				if($arr['group_id'])
				{
					$select->where("groupId = ?", $arr['group_id']);
				}elseif($my_group_id_arr[0]){
					$select->where("groupId IN (?)", $my_group_id_arr);
				}
				
				if($arr['province_id'])
				{
					$select->where("provinceId = ?", $arr['province_id']);
				}
				
				if($arr['city_id'])
				{
					$select->where("cityId = ?", $arr['city_id']);
				}elseif($my_city_id_arr[0]){
					$select->where("cityId IN (?)", $my_city_id_arr);
				}
				
				$rows = $this->dealers_db->fetchAll($select);
				
				foreach($rows as $row)
				{
					$dealer_ids[] = $row['id'];
				}
				
				$select = $this->dealers_db->select();
				$select->from("t_dealer_hxb", "id");
				
				if($arr['group_id'])
				{
					$select->where("groupId = ?", $arr['group_id']);
				}elseif($my_group_id_arr[0]){
					$select->where("groupId IN (?)", $my_group_id_arr);
				}
				
				if($arr['province_id'])
				{
					$select->where("provinceId = ?", $arr['province_id']);
				}
				
				if($arr['city_id'])
				{
					$select->where("cityId = ?", $arr['city_id']);
				}elseif($my_city_id_arr[0]){
					$select->where("cityId IN (?)", $my_city_id_arr);
				}
				
				$rows = $this->dealers_db->fetchAll($select);
				
				foreach($rows as $row)
				{
					$dealer_ids[] = $row['id'];
				}
				
				if(!empty($dealer_ids) && ($arr['factory_id'] || $my_factory_id_arr[0]))
				{
					$select = $this->dealers_db->select();
					$select->from("t_dealer_factory", "dealerId");
					
					if($arr['factory_id'])
					{
						$select->where("factoryId = ?", $arr['factory_id']);
					}elseif($my_factory_id_arr[0]){
						$select->where("factoryId IN (?)", $my_factory_id_arr);
					}
					
					$select->where("dealerId IN (?)", $dealer_ids);
					$rows = $this->dealers_db->fetchAll($select);
					
					$dealer_ids = array();
					
					if($rows)
					{
						foreach($rows as $row)
						{
							$dealer_ids[] = $row['dealerId'];
						}
					}
				}
			}
		}
		
		return $dealer_ids;
	}
	
	function GetMyLeaders($emp_id, $type=0) //type=1 只拉取最近的1位主管 2019.07.20
	{
		$result = array();
		
		if($emp_id)
		{
			$select = $this->hxb_db->select();
			$select->from("auth_emp", array("id", "parentEmpId", "realName"));
			$select->where("id = ?", $emp_id);
			$row = $this->hxb_db->fetchRow($select);
			
			if($row)
			{
				if($row['parentEmpId'])
				{
					$target_id = $row['parentEmpId'];
					
					while($target_id)
					{
						$select = $this->hxb_db->select();
						$select->from("auth_emp", array("id", "parentEmpId", "realName"));
						$select->where("id = ?", $target_id);
						$parent_row = $this->hxb_db->fetchRow($select);

						if($parent_row)
						{
							$result[] = array(
								"emp_id" => $parent_row['id'],
								"emp_name" => $parent_row['realName']
							);
							
							if(1 == $type) //type=1 只拉取最近的1位主管 2019.07.20
							{
								break;
							}

							if($parent_row['parentEmpId'])
							{
								$target_id = $parent_row['parentEmpId'];
							}else{
								$target_id = 0;
							}
						}
					}
				}
			}
		}
		
		return $result;
	}

    /**
     * 根据empid获取员工基本信息
     * @param $emp_id 员工ID
     * @return array
     */
	function GetAuthEmpInfo($emp_id){
        $result = array();

        if($emp_id)
        {
            $select = $this->hxb_db->select();
            $select->from("auth_emp", array("id", "username", "parentEmpId", "realName", "email"));
            $select->where("id = ?", $emp_id);
            $row = $this->hxb_db->fetchRow($select);
            return $row;
        }

        return $result;
    }

}